{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37a29ce1-64f8-4e68-b245-6b15ce1c8fbf",
   "metadata": {},
   "source": [
    "# 917作业:Python流程与执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb04e86c-a280-4e27-8d6b-316876ff90a9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f97f5f35-3cd5-4891-9071-f8e1ba9d71f4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0900e8ec814f4adb8ac38a5a6fb6fb8e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=250)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "39b9d0d7-0d72-4774-9c54-f6000f743245",
   "metadata": {},
   "outputs": [],
   "source": [
    "bob.forward(50)\n",
    "bob.left(90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "50723324-5837-4e9b-8150-c310902a0d7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "bob.forward(50)\n",
    "bob.left(90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f448ed13-e17d-49ab-b0a1-983470f1bda6",
   "metadata": {},
   "outputs": [],
   "source": [
    "bob.forward(50)\n",
    "bob.left(90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "42a98d25-695b-4c31-ba8b-ef5793682f26",
   "metadata": {},
   "outputs": [],
   "source": [
    "bob.forward(50)\n",
    "bob.left(90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "04045252-8d33-4011-a6d3-3f950b994b15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "703a95b907f049d4a2db06d4b783db97",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=250)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "for _ in range(3):\n",
    "    bob.forward(50)\n",
    "    bob.left(120)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "52970706-a621-4253-9a2a-a42583da5c12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bbab2a169ff149d0b227f6e101247925",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=250)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "for _ in range(10):\n",
    "    bob.forward(100)\n",
    "    bob.left(170)\n",
    "    bob.forward(100)\n",
    "    bob.left(170)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "28f089e8-e629-4584-b22d-78cbdacedee3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1e1d4e3565f44cdfbfe99ab0f16962aa",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=250)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "for _ in range(5):\n",
    "    bob.forward(144)\n",
    "    bob.right(144)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fcb48ec1-5716-4310-8be3-4effb1a46a24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a21175f997de4a228564f8b7f257a20f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=250)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "for _1 in range(36):\n",
    "    for _2 in range(4):\n",
    "        bob.forward(50)\n",
    "        bob.left(90)\n",
    "    bob.right(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7c90391b-31ab-4c49-85df-3bc6b57ca34d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "739ee08c00654e02bd9ee24570abf172",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=250)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "x=10\n",
    "for _1 in range(36):\n",
    "    for _2 in range(4):\n",
    "        bob.forward(x)\n",
    "        bob.left(90)\n",
    "    bob.right(10)\n",
    "    x += 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0d86b40c-3014-4560-aac3-de095d34476c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4bb5c2d5562545268f40acbf70a0b6c2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=250)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "a=10\n",
    "for _1 in range(36):\n",
    "    colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']\n",
    "    bob.pencolor(colors[x%6])\n",
    "    bob.width(x//100 + 1)\n",
    "    for _2 in range(4):\n",
    "        bob.forward(a)\n",
    "        bob.left(90)\n",
    "    bob.right(10)\n",
    "    a += 2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "182636b6-3ad0-402c-8c46-3ce7ea1850c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "53de62d82e8e44ad848f9f0ffbd03860",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=1000, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=1000)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "for _ in range(360):\n",
    "    bob.forward(5)\n",
    "    bob.left(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0df7aff2-f673-4586-860f-085c72a51d8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "33e83b5e85004634b6e82e34a5392476",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=400, width=800)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "比赛开始！\n",
      "有选手冲线了！\n",
      "恭喜！绿色乌龟赢得了比赛！\n"
     ]
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "import random\n",
    "import time\n",
    "\n",
    "myCanvas = turtle.Canvas(width=800, height=400)\n",
    "display(myCanvas)\n",
    "myTS = turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "\n",
    "TRACK_HEIGHT = 150\n",
    "FINISH_LINE_X = 350\n",
    "\n",
    "finish_line = turtle.Turtle(myTS)\n",
    "finish_line.penup()\n",
    "finish_line.goto(FINISH_LINE_X, -TRACK_HEIGHT)\n",
    "finish_line.pendown()\n",
    "finish_line.pensize(5)\n",
    "finish_line.color(\"red\")\n",
    "finish_line.setheading(90) \n",
    "finish_line.forward(TRACK_HEIGHT * 2)\n",
    "finish_line.hideturtle()\n",
    "\n",
    "divider = turtle.Turtle(myTS)\n",
    "divider.penup()\n",
    "divider.goto(-FINISH_LINE_X, 0)\n",
    "divider.pendown()\n",
    "divider.pensize(2)\n",
    "divider.color(\"white\")\n",
    "divider.setheading(0) \n",
    "divider.forward(FINISH_LINE_X * 2)\n",
    "divider.hideturtle()\n",
    "\n",
    "\n",
    "tortoise = turtle.Turtle(myTS)\n",
    "tortoise.shape(\"turtle\")\n",
    "tortoise.color(\"green\")\n",
    "tortoise.penup()\n",
    "tortoise.goto(-FINISH_LINE_X, -TRACK_HEIGHT / 2) \n",
    "tortoise.pendown()\n",
    "\n",
    "\n",
    "rabbit = turtle.Turtle(myTS)\n",
    "rabbit.shape(\"turtle\")\n",
    "rabbit.color(\"blue\")\n",
    "rabbit.penup()\n",
    "rabbit.goto(-FINISH_LINE_X, TRACK_HEIGHT / 2) \n",
    "rabbit.pendown() \n",
    "\n",
    "hold_canvas()\n",
    "\n",
    "print(\"比赛开始！\")\n",
    "\n",
    "while True:\n",
    "    tortoise_speed = random.randint(5, 7)\n",
    "    tortoise.forward(tortoise_speed)\n",
    "\n",
    "    if rabbit.xcor() > 300:\n",
    "        rabbit_speed = 0\n",
    "    elif tortoise.xcor() > FINISH_LINE_X: \n",
    "        rabbit_speed = random.randint(10, 20) \n",
    "        rabbit.forward(rabbit_speed)\n",
    "    else:\n",
    "        rabbit_speed = random.randint(10, 20)\n",
    "        rabbit.forward(rabbit_speed)\n",
    "\n",
    "    if tortoise.xcor() >  FINISH_LINE_X or rabbit.xcor() >  FINISH_LINE_X:\n",
    "        print(\"有选手冲线了！\")\n",
    "        break\n",
    "\n",
    "if tortoise.xcor() > rabbit.xcor():\n",
    "    print(\"恭喜！绿色乌龟赢得了比赛！\")\n",
    "else:\n",
    "    print(\"比赛结束！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4a636a6-cf95-40ab-a0de-fa7c015e6b32",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
